<#import "../tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="客户管理">

    <!--suppress ALL -->
    <form id="searchForm" class="form-panel" action="${basepath}/rest/manage/customer/loadData">
        <ul class="panel-content">
            <li>
                <div class="form-actions">
                    <div class="control-group controls span5">
                        <input type="text" class="input-normal  input-pad" name="name" placeholder="请输入关键字搜索">
                    </div>
                    <div class="form-actions  controls">
                        <button type="submit" class="button  button-primary">
                            查询
                        </button>
                        <a class="button button-success" href="${basepath}/rest/manage/customer/toAdd"
                           style="margin-left: 20px;">
                            添加
                        </a>
                    </div>
                </div>
            </li>
        </ul>
    </form>
    <div id="grid"></div>
<script>
    //查询登陆用户的名称
    var userID = "", isHeadman = "";
    $.ajax({
        type: 'GET',
        dataType: 'json',
        url: '/rest/manage/user/selectLoginUser',
        success: function (data) {
            userID = data.data.id;
            isHeadman = data.data.isHeadman;
            query();
        }
    })
    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            Select = BUI.Select, columns = "", store = "", grid = "";

    function query() {
        columns = [
            {
                title: '序号', elCls: 'center', dataIndex: 'id', width: '5%', renderer: function (value, obj, index) {
                    return index + 1;
                }
            },
            {title: '客户名称', dataIndex: 'name', elCls: 'center', width: '13%'},
            {title: '负责人', dataIndex: 'linkman', elCls: 'center', width: '10%'},
            {title: '联系电话', dataIndex: 'linkPhone', elCls: 'center', width: '10%'},
            {
                title: '联系地址', dataIndex: 'id', elCls: 'center', width: '20%', renderer: function (value, obj) {
                    return obj.province + obj.city + obj.area;
                }
            },
            {
                title: '合同详情', dataIndex: 'id', elCls: 'center', width: '7%', renderer: function (value, obj) {
                    return '<a href="${basepath}/rest/manage/contract/toList?customerID=' + value + '&customerName=' + obj.name + '">查看</a>';
                }
            },
            {title: '录入人员', dataIndex: 'userName', elCls: 'center', width: '10%'},
            {title: '录入时间', dataIndex: 'createTime', elCls: 'center', width: '15%'},
            {
                title: '操作', dataIndex: 'id', elCls: 'center', width: '10%', renderer: function (value, obj, index) {
                    return '<a href="${basepath}/rest/manage/customer/toEdit?id=' + value + '">编辑</a>&nbsp;&nbsp;<a href="javascript:deleteOne(' + value + ')">删除</a>';
                }
            }
        ];
        var url = "";
        isHeadman == 'n' ? url = '/rest/manage/customer/loadData?createUser=' + userID + '' : url = '/rest/manage/customer/loadData'
        store = new Store({
            url: url,
            autoLoad: true, //自动加载数据
            params: { //配置初始请求的参数
                length: '10',
                status: $("#status").val()
            },
            pageSize: 10,	// 配置分页数目
            root: 'list',
            totalProperty: 'total'
        });
        grid = new Grid.Grid({
            render: '#grid',
            columns: columns,
            loadMask: true, //加载数据时显示屏蔽层
            store: store,
            plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
            // 底部工具栏
            bbar: {
                pagingBar: true
            }
        });
        grid.render();
    }
    /**********************模糊搜索*************************/
    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });

    /**********************删除单个客户信息*************************/
    function deleteOne(value) {
        BUI.Message.Confirm('确定要执行删除操作？', function () {
            $.ajax({
                type: 'POST',
                url: '${basepath}/rest/manage/customer/deleteJson',
                dataType: 'json',
                data: {
                    id: value
                },
                success: function () {
                    var obj = new Object();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                }
            });
        }, 'question');
    }
</script>

</@page.pageBase>
